package com.ymttest.business.service.productdata.impl;

import java.util.Map;

import org.json.JSONObject;

import com.ymt.core.tool.Logger;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.ActivityOperate;
import com.ymttest.business.service.productdata.LiveOperate;
import com.ymttest.business.service.productdata.bean.ApplyProductBean;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class ProductFromJSONBean extends AbstractProductService{
	private String jsonBeanFile;
	
	private Map<String,Object> updateMap;
	
	public ProductFromJSONBean(Map<String,Object> updateMap){
		this.updateMap = updateMap;
	}
	
	public ProductFromJSONBean(){
	}
	
	
	protected String doAddProduct(ApplyProductBean applyProductBean) {
		//默认用新建live和activity方式
		if (this.getLive()==null){
			this.setLive(new NewLiveService(applyProductBean));
		}
		
		if (this.getActivity()==null){
			this.setActivity(new NewActivityService(applyProductBean));
		}
		
		if (this.jsonBeanFile==null || this.jsonBeanFile.isEmpty()){ 
			this.jsonBeanFile = "com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc11.json";
		}
		
		
		
		JSONObject addProductBean = JsonBeanHelper.getJsonBean(jsonBeanFile);
		
		
		JsonBeanHelper.updateJsonBean(addProductBean,MapUtil.hashMap("UserID", 
				applyProductBean.getSellerId(), "LoginID", applyProductBean.getSellerName()));
		
		if (updateMap!=null && !updateMap.isEmpty()){
			JsonBeanHelper.updateJsonBean(addProductBean,updateMap);
		}
		
		try {
			return SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
		} catch (Exception e) {
			Logger.fail(e);
		}
		
		return null;
	}
	
	
	public static class Builder{
		private LiveOperate lo = null;
		private ActivityOperate  ao= null;
		private Map<String,Object> updateMap = null;
		private String jsonBeanFile = null;
		
		public Builder JsonBeanFile(String jsonBeanFile){
			this.jsonBeanFile = jsonBeanFile;
			return this;
		}
		
		public Builder LiveOperate(LiveOperate lo){
			this.lo = lo;
			return this;
		}
		
		public Builder ActivityOperate(ActivityOperate ao){
			this.ao = ao;
			return this;
		}
		
		public Builder UpdateMap(Map<String,Object> updateMap){
			this.updateMap = updateMap;
			return this;
		}
		
		public ProductFromJSONBean build(){
			return new ProductFromJSONBean(this);
		}
		
		
	}
	 private ProductFromJSONBean(Builder b) {
		 this.setActivity(b.ao);
		 this.setLive(b.lo);
		 this.updateMap = b.updateMap;
		 this.jsonBeanFile = b.jsonBeanFile;
	 
	 }
}
